<?php
header("content-type:text/html;charset=utf-8");

class Login extends CI_Controller {
	function __construct(){
		parent::__construct();
		$this->load->helper(array('path','url'));
		$this->load->library(array('user_agent','transport','union'));
		session_start();
	}
	
	function index(){
		var_dump($_SESSION).die;
	}

	public function qq(){
		$this->load->library('qq');
		$this->qq->qq_login();
	}
	
	public function qq_back(){
		$this->load->library('qq');
		$result = $this->qq->qq_get_token();
		if ($result){
			$token = trim($result['access_token']);
			$openid = $this->qq->qq_get_openid($token);
			if($openid){
				$come_from = 'qq';
				$ret_user = $this->union->check_exists_openid($openid, $come_from);
				if ($ret_user){
					$this->union->refresh_user_token($ret_user['union_id'], $token, $result['expires_in']);
					$ret_user['is_core'] = 0;
					$this->union->autoLogin($ret_user);
				}
				else{
					$info = $this->qq->qq_get_info($token, $openid);
					if ($info['ret'] == 0){
						$time = time();
						
						$union_user = array(
											'token' => $token,
									  		'openid' => $openid,
									  		'expire' => $result['expires_in'],
									  		'add_time' => $time,
											'last_update' => $time,
									 		'user_name' => $this->myhtmlspecialchars(trim($info['nickname'])) ,//实体化引号,
											'sex' => trim($result['gender']) == '女' ? 2 : (trim($result['gender']) == '保密' ? 0 : 1),
											'come_from' => $come_from
						);
						$ret_user = $this->union->add_union_account($union_user);
						$ret_user['is_core'] = 0;
						$this->union->autoLogin($ret_user);
					}
				}
			}
		}
		$this->union->page_redirect();
	}
	
	function myhtmlspecialchars($str){
		$str= str_replace("'","&#039;",$str);
		$str= str_replace("\"","&quot;",$str);
		return $str;
	}
	
	private function alipay(){
		$this->load->library('alipay');
		$return_url = 'http://passport.beyond.cn/index.php/login/alipay_back/';
		//$return_url = 'http://me.beyond.cn/passport/index.php/login/alipay_back/';
		$this->alipay->alipay_login($return_url);
	}
	
	public function alipay_back(){
		$this->load->library('alipay');
		if($_GET['is_success'] == 'T'){
			if($this->alipay->validate_sign($_GET, $_GET['sign'])){
				
				$openid = trim($_GET['user_id']);
				$token = trim($_GET['token']);
				
				$come_from = 'alipay';
				
				$ret_user = $this->union->check_exists_openid($openid, $come_from);
				$nick_name = trim($_GET['real_name']);
				if ($ret_user){
					$ret_user['user_name'] = $this->union->refresh_user_token($ret_user['union_id'], $token, 0, $nick_name);
					
					$ret_user['is_core'] = 0;
					$this->union->autoLogin($ret_user);
				}
				else{
					
					$time = time();
					
					$union_user = array(
										'token' => $token,
								  		'openid' => $openid,
								  		'expire' => 30*60,//有效期为30分钟
								  		'add_time' => $time,
										'last_update' => $time,
								 		'user_name' => $nick_name,
										'sex' => 0,
										'come_from' => $come_from
								  );
					
					$ret_user = $this->union->add_union_account($union_user);
					$ret_user['is_core'] = 0;
					$this->union->autoLogin($ret_user);
					
				}
			}
		}
		
		$this->union->page_redirect();
	}
	
	public function neteasy(){
		$this->load->library('oauth');
		
		$oauth = new Oauth(CONSUMER_KEY, CONSUMER_SECRET);
		
		$request_token = $oauth->getRequestToken();
		$_SESSION ['163_token'] = $request_token;
		$aurl = $oauth->getAuthorizeURL($request_token ['oauth_token'], 'http://www.geili0574.com/index.php/login/neteasy_back' );
		Header("Location: ".$aurl);exit;
	}
	
	public function neteasy_back(){
		$this->load->library('oauth');
		$oauth = new Oauth( CONSUMER_KEY, CONSUMER_SECRET , $_SESSION['163_token']['oauth_token'] , $_SESSION['163_token']['oauth_token_secret']  );
		if ($access_token = $oauth->getAccessToken(  $_REQUEST['oauth_token'] ) )
		{
			
			$tblog = new TBlog(CONSUMER_KEY, CONSUMER_SECRET,$access_token['oauth_token'],$access_token['oauth_token_secret']);
			$me = $tblog->verify_credentials();
			$username = $me['name'];
			$email = $me['email'];
			$gender = $me['gender'];
			
			$openid = $me['id'];
			if (empty($openid))$this->union->page_redirect();
			
			$token = $access_token['oauth_token'];
			$expires = 0;
		    $a = preg_split('/[\@]/',$email);
		    
		    $come_from = '163';
		    
			$ret_user = $this->union->check_exists_openid($openid, $come_from);
			
			$nick_name = trim($a['0']);
			
			if ($ret_user){
				$this->union->refresh_user_token($ret_user['union_id'], $token, $expires);
				
				$ret_user['is_core'] = 0;
				$this->union->autoLogin($ret_user);
			}
			else{
				
				$time = time();
				$union_user = array(
														'token' => $token,
												  		'openid' => $openid,
												  		'expire' => $expires,
												  		'add_time' => $time,
														'last_update' => $time,
														'tmpid' => $username,
												 		'user_name' => $nick_name,//$me['name']
														'sex' => $gender,
														'come_from' => $come_from,
				                                        'email' => $email
				);
				$ret_user = $this->union->add_union_account($union_user);
				$ret_user['is_core'] = 0;
				$this->union->autoLogin($ret_user);
			}
		}
		//uther 163 union login 2012.5.24 end  
		$this->union->page_redirect();
	}
	
	private function douban(){
	}
	
	private function renren(){
	}
	
	public function sina(){
		$this->load->library('sinaauthv2');
		$o = new Sinaauthv2( WB_AKEY , WB_SKEY );
		$url = $o->getAuthorizeURL(WB_CALLBACK_URL);
		Header("Location: ".$url);exit;
	}
	
	public function sina_back(){
		
		$this->load->library('sinaauthv2');
		
		$this->load->library('sina');
		$o = new Sinaauthv2( WB_AKEY , WB_SKEY );
		
		if (isset($_GET['code'])) {
			$keys = array();
			$keys['code'] = $_GET['code'];
			$keys['redirect_uri'] = WB_CALLBACK_URL;
			try {
				$ret_token = $o->getAccessToken( 'code', $keys ) ;
				if ($ret_token) {
					$token = $ret_token['access_token'];
					$expires = $ret_token['expires_in'];
				    $c = new Sina( WB_AKEY , WB_SKEY , $token );
				    $uid_get = $c->get_uid();
				    $uid = $uid_get['uid'];
				    $user_message = $c->show_user_by_id( $uid);
				    $username = $user_message['name'];
				    $openid = $user_message['id'];
				    if (empty($openid))$this->union->page_redirect();
				    $come_from = 'sina';
				    $ret_user = $this->union->check_exists_openid($openid, $come_from);
					$nick_name = trim($user_message['screen_name']);
					if ($ret_user){
						$this->union->refresh_user_token($ret_user['union_id'], $token, $expires, $nick_name);
						$ret_user['is_core'] = 0;
						$this->union->autoLogin($ret_user);
					}
					else{
						$time = time();
						$union_user = array(
													'token' => $token,
											  		'openid' => $openid,
											  		'expire' => $expires,
											  		'add_time' => $time,
													'last_update' => $time,
													'tmpid' => $username,
											 		'user_name' => $nick_name,
													'sex' => trim($user_message['gender']) == 'm' ? 1 : (trim($user_message['gender']) == 'n' ? 0 : 2),
													'come_from' => $come_from
											  );
						$ret_user = $this->union->add_union_account($union_user);
						$ret_user['is_core'] = 0;
						$this->union->autoLogin($ret_user);
					}
				}
			} catch (OAuthException $e) {
			}
		}
		$this->union->page_redirect();
	}
	
	private function taobao(){
		$this->load->library('taobaoauthv2');
		$o = new Taobaoauthv2( APPKEY , SECRETKEY );
		$state = md5 ( uniqid ( mt_rand (), true ) );
		$url = $o->getAuthorizeURL ( CALLBACK, 'code', $state );
		Header("Location: ".$url);exit;
	}
	
	public function taobao_back(){
		$this->load->library('taobaoauthv2');
		$this->load->library('taobao');
		$o = new Taobaoauthv2( APPKEY , SECRETKEY );
		if (isset($_GET['code'])) {
			$keys = array();
			$keys['code'] = $_GET['code'];
			$keys['redirect_uri'] = CALLBACK;
			
				$ret_token = $o->getAccessToken( 'code', $keys ) ;
				
				if ($ret_token) {
					
					$token = $ret_token['access_token'];
					$expires = $ret_token['expires_in'];
				    
				    $openid = $ret_token['taobao_user_id'];
				    if (empty($openid))$this->union->page_redirect();
				    
				    $come_from = 'taobao';
				    
				    $ret_user = $this->union->check_exists_openid($openid, $come_from);
				    
					$nick_name = trim(urldecode($ret_token['taobao_user_nick']));
					
					if ($ret_user){
						$this->union->refresh_user_token($ret_user['union_id'], $token, $expires, $nick_name);
						
						$ret_user['is_core'] = 0;
						$this->union->autoLogin($ret_user);
					}
					else{
						$time = time();
						$union_user = array(
													'token' => $token,
											  		'openid' => $openid,
											  		'expire' => $expires,
											  		'add_time' => $time,
													'last_update' => $time,
											 		'user_name' => $nick_name,
													'sex' => 0,
													'come_from' => $come_from
											  );
						$ret_user = $this->union->add_union_account($union_user);
						$ret_user['is_core'] = 0;
						$this->union->autoLogin($ret_user);
					}
				}
				
		}
		page_redirect();
	}

	function logout(){
		session_destroy();
		$url = $this->agent->referrer();
		$this->jump($url);
	}

	private function jump($url){
		unset($_SESSION['login_back_url']);
		header('Location:'.$url);
	}
}